package com.cn.trip.DbDao;

import com.cn.trip.DaoUtil.DBHelper;
import com.cn.trip.bean.Account;
import com.cn.trip.bean.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
	private ResultSet rs;
	private PreparedStatement ps;
	private Connection conn;
	
	public User selectUser(String username, String password) {
		String sql="select * from user where username=? and password=?";
		conn= DBHelper.getConnection();
		User user=null;
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2,password);
			rs=ps.executeQuery();
			if(rs.next()) {
				user=new User();
				user.setId(rs.getInt(1));
				user.setUsername(rs.getString(2));
				user.setPassword(rs.getString(3));
				if(rs.getString(4)==null) {
					user.setCart(null);
				}else {
					String new_sql="select * from account where cart_num="+rs.getString(4);
					ps=conn.prepareStatement(new_sql);
					rs=ps.executeQuery();
					if(rs.next()) {
						Account account=new Account(rs.getInt(1), rs.getString(2),rs.getString(3),rs.getFloat(4));
						user.setCart(account);
					}
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				DBHelper.closeAll(rs, ps, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return user;
	}
	public int  UserRegister(String username,String password) {
		String sql="insert into user (username,password) values(?,?)";
		conn=DBHelper.getConnection();
		int flag=0;
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1,username);
			ps.setString(2,password);
			flag=ps.executeUpdate();
		} catch (SQLException e) {
		
			e.printStackTrace();
		}finally {
			try {
				DBHelper.closeAll(rs, ps, conn);
			} catch (SQLException e) {
			
				e.printStackTrace();
			}
		}
		
		return flag;
	}
	public List<User> getAllUser(){
		String sql="select * from user";
		List<User> list=new ArrayList<User>();
		conn=DBHelper.getConnection();
		try {
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()) {
				User user=new User();
				user=new User();
				user.setId(rs.getInt(1));
				user.setUsername(rs.getString(2));
				user.setPassword(rs.getString(3));
				
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	public int delete(int id) {
		String sql="delete from user where id="+id;
		conn=DBHelper.getConnection();
		int flag=0;
		try {
			ps=conn.prepareStatement(sql);
			flag=ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public User getUser(int id) {
		// TODO Auto-generated method stub
		String sql="select * from user where id="+id;
		conn=DBHelper.getConnection();
		User user=null;
		try {
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()) {
				user=new User();
				user.setId(rs.getInt(1));
				user.setUsername(rs.getString(2));
				user.setPassword(rs.getString(3));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return user;
	}
	
	public int alterUser(User user) {
		String sql="update user set username=?,password=?,name=? where id=?";
		conn=DBHelper.getConnection();
		int flag=0;
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1,user.getUsername());
			ps.setString(2, user.getPassword());
//			ps.setString(3,user.getName());
			ps.setInt(4, user.getId());
			flag=ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	public	int boundCart(int user_id,String card) {
		String sql="update user set cart_num=? where id=?";
		conn=DBHelper.getConnection();
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, card);
			ps.setInt(2, user_id);
			return ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
}
